
import QtQuick 2.14
import QtQuick.Controls 2.14
import QtQuick.Layouts 1.14
import ModbusDemo 1.0

Page {
    id: clientPage
    padding: 10

    // 添加编辑状态管理
    property bool isEditing: false

    function startEditing() {
        if (modbusClient && modbusClient.connected && !isEditing) {
            isEditing = true;
            modbusClient.stopReadTimer();
        }
    }

    function finishEditing() {
        if (modbusClient && modbusClient.connected && isEditing) {
            isEditing = false;
            modbusClient.startReadTimer();
        }
    }

    ColumnLayout {
        anchors.fill: parent
        spacing: 20

        // 连接控制区域
        GroupBox {
            title: qsTr("Connection")
            Layout.fillWidth: true

            ColumnLayout {
                anchors.fill: parent
                spacing: 10

                RowLayout {
                    Layout.fillWidth: true

                    Label {
                        text: qsTr("Server IP:")
                        Layout.preferredWidth: 80
                    }

                    TextField {
                        id: serverAddress
                        text: "127.0.0.1"
                        Layout.preferredWidth: 150
                        selectByMouse: true
                    }

                    Label {
                        text: qsTr("Port:")
                        Layout.preferredWidth: 40
                    }

                    SpinBox {
                        id: portSpinBox
                        from: 1
                        to: 65535
                        value: 502
                        Layout.preferredWidth: 100
                    }

                    Button {
                        text: modbusClient.connected ? qsTr("Disconnect") : qsTr("Connect")
                        onClicked: {
                            if (!modbusClient.connected) {
                                modbusClient.connectToServer(serverAddress.text, portSpinBox.value)
                            } else {
                                modbusClient.disconnectFromServer()
                            }
                        }
                    }

                    Label {
                        text: qsTr("Status: ") + modbusClient.status
                        color: modbusClient.connected ? "green" : "red"
                        Layout.fillWidth: true
                    }
                }
            }
        }

        // 数据显示和控制区域
        GroupBox {
            title: qsTr("Device Data")
            Layout.fillWidth: true
            Layout.fillHeight: true
            enabled: modbusClient.connected

            ScrollView {
                anchors.fill: parent
                clip: true

                ColumnLayout {
                    width: parent.width
                    spacing: 15

                    // ID
                    RowLayout {
                        Layout.fillWidth: true
                        Label { 
                            text: qsTr("Device ID:")
                            Layout.preferredWidth: 120
                        }
                        SpinBox {
                            value: modbusClient.deviceId
                            from: 0
                            to: 65535
                            onValueModified: {
                                startEditing()
                                modbusClient.writeDeviceId(value)
                                finishEditing()
                            }
                        }
                    }

                    // 设备名称
                    RowLayout {
                        Layout.fillWidth: true
                        Label { 
                            text: qsTr("Device Name:")
                            Layout.preferredWidth: 120
                        }
                        TextField {
                            text: modbusClient.deviceName
                            Layout.preferredWidth: 200
                            selectByMouse: true
                            onFocusChanged: {
                                if (focus) startEditing()
                            }
                            onEditingFinished: {
                                if (!focus) {
                                    modbusClient.writeDeviceName(text)
                                    finishEditing()
                                }
                            }
                        }
                    }

                    // 数值
                    RowLayout {
                        Layout.fillWidth: true
                        Label { 
                            text: qsTr("Value:")
                            Layout.preferredWidth: 120
                        }
                        SpinBox {
                            id: valueSpinBox
                            value: modbusClient.deviceValue * 10
                            from: 0
                            to: 10000
                            stepSize: 1
                            onValueModified: {
                                startEditing()
                                modbusClient.writeDeviceValue(value / 10)
                                finishEditing()
                            }
                            textFromValue: function(value) {
                                return (value / 10).toFixed(1)
                            }
                            valueFromText: function(text) {
                                return parseFloat(text) * 10
                            }
                        }
                    }

                    // 温度
                    RowLayout {
                        Layout.fillWidth: true
                        Label { 
                            text: qsTr("Temperature:")
                            Layout.preferredWidth: 120
                        }
                        SpinBox {
                            id: tempSpinBox
                            value: modbusClient.temperature * 10
                            from: -500
                            to: 1000
                            stepSize: 1
                            onValueModified: {
                                startEditing()
                                modbusClient.writeTemperature(value / 10)
                                finishEditing()
                            }
                            textFromValue: function(value) {
                                return (value / 10).toFixed(1)
                            }
                            valueFromText: function(text) {
                                return parseFloat(text) * 10
                            }
                        }
                        Label { text: "°C" }
                    }

                    // 湿度
                    RowLayout {
                        Layout.fillWidth: true
                        Label { 
                            text: qsTr("Humidity:")
                            Layout.preferredWidth: 120
                        }
                        SpinBox {
                            id: humiditySpinBox
                            value: modbusClient.humidity * 10
                            from: 0
                            to: 1000
                            stepSize: 1
                            onValueModified: {
                                startEditing()
                                modbusClient.writeHumidity(value / 10)
                                finishEditing()
                            }
                            textFromValue: function(value) {
                                return (value / 10).toFixed(1)
                            }
                            valueFromText: function(text) {
                                return parseFloat(text) * 10
                            }
                        }
                        Label { text: "%" }
                    }

                    // 压力
                    RowLayout {
                        Layout.fillWidth: true
                        Label { 
                            text: qsTr("Pressure:")
                            Layout.preferredWidth: 120
                        }
                        SpinBox {
                            value: modbusClient.pressure
                            from: 800
                            to: 1200
                            onValueModified: {
                                startEditing()
                                modbusClient.writePressure(value)
                                finishEditing()
                            }
                        }
                        Label { text: "hPa" }
                    }

                    // 设备状态
                    RowLayout {
                        Layout.fillWidth: true
                        Label { 
                            text: qsTr("Device Status:")
                            Layout.preferredWidth: 120
                        }
                        Switch {
                            id: deviceStatusSwitch
                            checked: modbusClient.deviceStatus
                            onClicked: {
                                startEditing()
                                modbusClient.writeDeviceStatus(checked)
                                finishEditing()
                            }
                        }
                    }

                    // 报警状态
                    RowLayout {
                        Layout.fillWidth: true
                        Label { 
                            text: qsTr("Alarm Status:")
                            Layout.preferredWidth: 120
                        }
                        SpinBox {
                            value: modbusClient.alarmStatus
                            from: 0
                            to: 255
                            onValueModified: {
                                startEditing()
                                modbusClient.writeAlarmStatus(value)
                                finishEditing()
                            }
                        }
                    }

                    // 描述信息
                    ColumnLayout {
                        Layout.fillWidth: true
                        spacing: 5

                        Label { 
                            text: qsTr("Description:")
                        }
                        TextArea {
                            id: descriptionArea
                            text: modbusClient.description
                            Layout.fillWidth: true
                            wrapMode: TextEdit.Wrap
                            selectByMouse: true
                            onFocusChanged: {
                                if (focus) {
                                    startEditing()
                                } else {
                                    modbusClient.writeDescription(text)
                                    finishEditing()
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    // 错误提示对话框
    Popup {
        id: errorPopup
        anchors.centerIn: parent
        width: 400
        modal: true
        focus: true
        closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutside

        contentItem: ColumnLayout {
            spacing: 20

            Label {
                id: errorLabel
                text: ""
                wrapMode: Text.WordWrap
                Layout.fillWidth: true
            }

            Button {
                text: qsTr("Close")
                onClicked: errorPopup.close()
                Layout.alignment: Qt.AlignRight
            }
        }
    }

    // 错误处理
    Connections {
        target: modbusClient
        function onErrorOccurred(error) {
            errorLabel.text = error
            errorPopup.open()
        }
    }
}
